package com.sims.daoImp;

import java.util.List;
import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.sims.bean.Post;
import com.sims.dao.IPostDAO;

/**
 * A data access object (DAO) providing persistence and search support for Post
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see com.sims.bean.Post
 * @author MyEclipse Persistence Tools
 */

public class PostDAO extends HibernateDaoSupport implements IPostDAO {
	private static final Logger log = LoggerFactory.getLogger(PostDAO.class);
	// property constants
	public static final String THID = "thid";
	public static final String IS_THEME = "isTheme";
	public static final String AUTHOR = "author";
	public static final String TITLE = "title";
	public static final String AUTHOR_ID = "authorId";
	public static final String BODY = "body";
	public static final String AUTHOR_IDENTITY = "authorIdentity";
	public static final String STIME = "stime";

	@Override
	protected void initDao() {
		// do nothing
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IPostDAO#save(com.sims.bean.Post)
	 */
	@Override
	public void save(Post transientInstance) {
		log.debug("saving Post instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IPostDAO#delete(com.sims.bean.Post)
	 */
	@Override
	public void delete(Post persistentInstance) {
		log.debug("deleting Post instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IPostDAO#findById(java.lang.Integer)
	 */
	@Override
	public Post findById(java.lang.Integer id) {
		log.debug("getting Post instance with id: " + id);
		try {
			Post instance = (Post) getHibernateTemplate().get(
					"com.sims.bean.Post", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IPostDAO#findByExample(com.sims.bean.Post)
	 */
	@Override
	public List findByExample(Post instance) {
		log.debug("finding Post instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IPostDAO#findByProperty(java.lang.String, java.lang.Object)
	 */
	@Override
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Post instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Post as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IPostDAO#findByThid(java.lang.Object)
	 */
	@Override
	public List findByThid(Object thid) {
		return findByProperty(THID, thid);
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IPostDAO#findByIsTheme(java.lang.Object)
	 */
	@Override
	public List findByIsTheme(Object isTheme) {
		return findByProperty(IS_THEME, isTheme);
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IPostDAO#findByAuthor(java.lang.Object)
	 */
	@Override
	public List findByAuthor(Object author) {
		return findByProperty(AUTHOR, author);
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IPostDAO#findByTitle(java.lang.Object)
	 */
	@Override
	public List findByTitle(Object title) {
		return findByProperty(TITLE, title);
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IPostDAO#findByAuthorId(java.lang.Object)
	 */
	@Override
	public List findByAuthorId(Object authorId) {
		return findByProperty(AUTHOR_ID, authorId);
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IPostDAO#findByBody(java.lang.Object)
	 */
	@Override
	public List findByBody(Object body) {
		return findByProperty(BODY, body);
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IPostDAO#findByAuthorIdentity(java.lang.Object)
	 */
	@Override
	public List findByAuthorIdentity(Object authorIdentity) {
		return findByProperty(AUTHOR_IDENTITY, authorIdentity);
	}
	
	public List findByTime(Object stime) {
		return findByProperty(STIME, stime);
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IPostDAO#findAll()
	 */
	@Override
	public List findAll() {
		log.debug("finding all Post instances");
		try {
			String queryString = "from Post";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IPostDAO#merge(com.sims.bean.Post)
	 */
	@Override
	public Post merge(Post detachedInstance) {
		log.debug("merging Post instance");
		try {
			Post result = getHibernateTemplate().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IPostDAO#attachDirty(com.sims.bean.Post)
	 */
	@Override
	public void attachDirty(Post instance) {
		log.debug("attaching dirty Post instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IPostDAO#attachClean(com.sims.bean.Post)
	 */
	@Override
	public void attachClean(Post instance) {
		log.debug("attaching clean Post instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static IPostDAO getFromApplicationContext(ApplicationContext ctx) {
		return (IPostDAO) ctx.getBean("PostDAO");
	}
}